/**
 * 提示
 * @param {Object} options
 * @param {String} options.title 提示内容
 * @param {String} options.icon 提示图标
 * @param {Number} options.duration 提示时长
 * @param {Boolean} options.mask 是否显示遮罩
 */
export const toast = ({
  title = '数据加载中...',
  icon = 'none',
  duration = 2000,
  mask = true
} = {}) => {
  wx.showToast({
    title,
    icon,
    duration,
    mask
  })
}

/**
 * 弹窗
 * @param {Object} options
 * @param {String} options.title 标题
 * @param {String} options.content 内容
 * @param {String} options.confirmColor 确认按钮颜色
 * @param {Boolean} options.showCancel 是否显示取消按钮
 */
export const modal = (options = {}) => {
  return new Promise((resolve) => {
    const defaultOptions = {
      title: '提示',
      content: '您确定执行该操作吗？',
      confirmColor: '#f3514f',
      showCancel: true
    }
    // 合并参数
    const mergeOptions = Object.assign({}, defaultOptions, options)

    wx.showModal({
      ...mergeOptions,
      complete({ confirm, cancel }) {
        confirm && resolve(true)
        cancel && resolve(false)
      }
    })
  })
}

/**
 * 打开授权设置
 * @returns {Promise} 成功/失败回调
 * https://developers.weixin.qq.com/miniprogram/dev/api/base/system/wx.openAppAuthorizeSetting.html
 */
export const openAppAuthorizeSetting = () => {
  return new Promise((resolve, reject) => {
    wx.openAppAuthorizeSetting({
      success(res) {
        resolve(res)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}

/**
 * 获取窗口信息
 * @returns {Object} 窗口信息
 * https://developers.weixin.qq.com/miniprogram/dev/api/base/system/wx.getWindowInfo.html
 */
export const getWindowInfo = () => {
  return wx.getWindowInfo()
}

/**
 * 获取设备设置
 * @returns {Object} 系统信息
 * https://developers.weixin.qq.com/miniprogram/dev/api/base/system/wx.getSystemSetting.html
 */
export const getSystemInfo = () => {
  return wx.getSystemSetting()
}

/**
 * 获取设备信息
 * @returns {Object} 设备信息
 * https://developers.weixin.qq.com/miniprogram/dev/api/base/system/wx.getDeviceInfo.html
 */
export const getDeviceInfo = () => {
  return wx.getDeviceInfo()
}

/**
 * 获取设备性能得分和机型档位数据
 * @returns {Object} 设备性能得分和机型档位数据
 * https://developers.weixin.qq.com/miniprogram/dev/api/base/system/wx.getDeviceBenchmarkInfo.html
 */
export const getDeviceBenchmarkInfo = () => {
  return new Promise((resolve, reject) => {
    wx.getDeviceBenchmarkInfo({
      success(res) {
        resolve(res)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}

/**
 * 获取微信APP授权设置
 * @returns {Object} 微信APP授权信息
 * https://developers.weixin.qq.com/miniprogram/dev/api/base/system/wx.getAppAuthorizeSetting.html
 */
export const getAppAuthorizeSetting = () => {
  return wx.getAppAuthorizeSetting()
}

/**
 * 在当前页面显示导航条加载动画
 * @returns {Object} 导航栏加载动画
 * https://developers.weixin.qq.com/miniprogram/dev/api/ui/navigation-bar/wx.showNavigationBarLoading.html
 */
export const showNavigationBarLoading = () => {
  return wx.showNavigationBarLoading()
}

/**
 * 隐藏导航条加载动画
 * @returns {Object} 导航栏加载动画
 * https://developers.weixin.qq.com/miniprogram/dev/api/ui/navigation-bar/wx.hideNavigationBarLoading.html
 */
export const hideNavigationBarLoading = () => {
  return wx.hideNavigationBarLoading()
}

/**
 * 设置导航栏标题
 * @param {String} title 标题
 * https://developers.weixin.qq.com/miniprogram/dev/api/ui/navigation-bar/wx.setNavigationBarTitle.html
 */
export const setNavigationBarTitle = (title) => {
  return wx.setNavigationBarTitle({ title })
}

/**
 * 设置导航栏颜色
 * @param {Object} options 相关配置
 * https://developers.weixin.qq.com/miniprogram/dev/api/ui/navigation-bar/wx.setNavigationBarColor.html
 */
export const setNavigationBarColor = (options = {}) => {
  const defaultOptions = {
    frontColor: '#ffffff',
    backgroundColor: '#000000',
    animation: {
      duration: 0,
      timingFunc: 'linear'
    }
  }
  return wx.setNavigationBarColor(Object.assign({}, defaultOptions, options))
}
